home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Extras / NetObjects Fusion / NOF10.exe / data1.cab / FSI / lib / nof / util / ResourceBundle.js < prev    next >
Encoding:
Text File  |  2007-04-11  |  7.8 KB  |  258 lines

  1. /****i* SOURCE_FILE/INFO
  2.   *
  3.   * NAME
  4.   *  ResourceBundle.js
  5.   *
  6.   * USAGE
  7.   *  Part of Netobjects JavaScript Library.
  8.   *
  9.   * COPYRIGHT
  10.   *  Copyright ⌐ 2000-2005 Website Pros, Inc.
  11.   *  All Rights Reserved.
  12.   *
  13.   *  This is an unpublished work protected by Website Pros, Inc.
  14.   *  as a trade secret, and is not to be used or disclosed except as
  15.   *  expressly provided in a written license agreement executed by
  16.   *  you and Website Pros, Inc.
  17.   *
  18.   *      <copyright@websitepros.com>
  19.   *
  20.   * NOTES
  21.   *  JavaScript code.
  22.   *
  23.   *****/
  24.  
  25. if (!IS.isModuleInitialized("IS.NOF.UTIL.ResourceBundle"))
  26.     /****h* NOF_JavaScript_Library/NOF.UTIL.ResourceBundle
  27.     *
  28.     * NAME
  29.     *  NOF.UTIL.ResourceBundle
  30.     *
  31.     * DESCRIPTION
  32.     *   Resource bundles contain locale-specific objects. 
  33.     *   When your program needs a locale-specific resource, 
  34.     *   a String for example, your program can load it from 
  35.     *   the resource bundle that is appropriate for the current 
  36.     *   user's locale. In this way, you can write program code 
  37.     *   that is largely independent of the user's locale isolating 
  38.     *   most, if not all, of the locale-specific information in resource bundles.
  39.     * 
  40.     * External dependencies: NOF.App, NOF.UTIL.Locale, NOF.IO.File, NOF.UTIL.PropertyResourceBundle
  41.     ****/
  42.     
  43.   /**
  44.     * Constructor
  45.     */    
  46.     function UTIL_ResourceBundle(){        
  47.         this.__proto__ = UTIL_ResourceBundle.prototype;
  48.     }  
  49.     {
  50.         var nof_unicode_re = /\\u/;  
  51.         function NOF_UnicodeEval(val) {
  52.             if (nof_unicode_re.test(val)) {                
  53.                 
  54.                 var expVal = ""; 
  55.                 
  56.                 try {
  57.                     val = val.replace(/\"/g, '\\"');
  58.                         val = val.replace(/\'/g, "\\'");
  59.                         eval('expVal = "' + val + '";');
  60.                 } catch(e) {
  61.                     return val;
  62.                 }
  63.                 return expVal;
  64.             } else {
  65.                 return val;    
  66.             }
  67.         }
  68.     
  69.         var method = UTIL_ResourceBundle.prototype;
  70.         
  71.         /**
  72.           * Return a NOF.UTIL.PropertyResourceBundle based on the given resource content <br>
  73.           * @param content - the resource content
  74.           * @param toRetObj - the property resource bundle to be instantiated 
  75.           * (if not specified a new object will be created)
  76.           *
  77.           **/
  78.         method.getProperties = function ( /*string*/ content, /*[ NOF.UTIL.PropertyResourceBundle*/ toRetObj /*]*/, /*NOF.UTIL.Locale*/ locale) {
  79.             if (arguments.length < 2)
  80.                 toRetObj =  new NOF.UTIL.PropertyResourceBundle( "anon" );
  81.             // escape native2ascii begin of file marker
  82.             if (content.indexOf("\\ufeff") == 0) {
  83.                 content = content.substring("\\ufeff".length);
  84.             }
  85.             var hashList = content.split('\n');
  86.             
  87.             var separatorIndex, key, val;
  88.             var line;
  89.             for (var i=0; i<hashList.length; i++) { 
  90.                 line = hashList[i];
  91.                 if (line.length==0 || line.indexOf("#")==0) {
  92.                     continue;
  93.                 }
  94.                 
  95.                 separatorIndex = line.indexOf('=');
  96.                 if (separatorIndex>-1) {
  97.                     key = line.substring(0, separatorIndex);
  98.                     //key = key.replace (/\s/g, "");
  99.                     key = key.trim();
  100.                     val = line.substring( separatorIndex+1, line.length);
  101.  
  102.                     if (val.lastIndexOf("\\") == val.length-1) {
  103.                         val = NOF_UnicodeEval( val.substring(0,val.length-1)) + "\n";
  104.                         
  105.                         while ( (i + 1 < hashList.length) ) {
  106.                             i++;
  107.                             line = hashList[i];
  108.                             if (line.length==0) break;
  109.                             if (line.charAt(line.length - 1) == '\\') {
  110.                                 val += NOF_UnicodeEval(line.substring(0, line.length-1)) + "\n";
  111.                             } else {
  112.                                 val += NOF_UnicodeEval(line);
  113.                                 break;
  114.                             }                            
  115.                         }
  116.                     } else {
  117.                         val = NOF_UnicodeEval(val);
  118.                     }
  119.                     
  120.                     //pgLogger.info(key + "=" + val);
  121.                     
  122.                     /*
  123.                     while (val.lastIndexOf("\\") == val.length-1) {
  124.                     i++;
  125.                     val = val.substring(0, val.length-1) + "\n" + hashList[i];
  126.                     }*/
  127.                     if (key == "@import") {
  128.                         
  129.                         //search for keys, separated by comma, enclosed in brakets { }
  130.                         var restrictToKeys = null;
  131.                         if (val.charAt(val.length-1) == "}") {
  132.                             var bIndex = val.indexOf("{");
  133.                             restrictToKeys = val.substring(bIndex + 1, val.length-1).split(",");
  134.                             val = val.substring(0, bIndex);
  135.                         }
  136.                         var importedProp = this.getBundle(val, locale);
  137.                         if (importedProp == null) {
  138.                             continue;
  139.                         }
  140.                         var importedKeys = (restrictToKeys) ? restrictToKeys : importedProp.getKeys();
  141.                         for (var j=0; j < importedKeys.length; j++) {
  142.                             //alert(toRetObj.getID() + " - key:" + importedKeys[j] + "=\n" + importedProp.getProperty(importedKeys[j]) );
  143.                             toRetObj.setProperty(importedKeys[j], importedProp.getProperty(importedKeys[j]));
  144.                         }
  145.                     } else {
  146.                         // use key=$property=anAlreadyDefinedKey
  147.                         if (val.indexOf("$property=") == 0) {
  148.                             var sameValueKey = val.substring(10); //"$property=".length == 10
  149.                             val = toRetObj.getProperty(sameValueKey);
  150.                         }
  151.                         //
  152.                         toRetObj.setProperty(key,val);
  153.                     }
  154.                 } 
  155.             }
  156.             
  157.             return toRetObj;    
  158.         }
  159.         
  160.         /**
  161.           * Return a NOF.UTIL.PropertyResourceBundle based on the given resource file <br>
  162.           * @param resource - the path and name (without extension) of the resource file
  163.           * @param locale - the locale
  164.           **/
  165.         method.getBundle  = function (/*string*/ resource, /*NOF.UTIL.Locale*/ locale) {
  166.             var usingDefault = false;
  167.             var defaultLocale = (new NOF.UTIL.Locale()).getDefault();
  168.             
  169.             if (defaultLocale.equals(locale) || locale == null){
  170.                 locale = defaultLocale;
  171.                 usingDefault = true;
  172.             }
  173.             
  174.             var resourceName   = "";
  175.             var resourceToFind = "";
  176.             
  177.             var libPathsArray = NOF.App.getLibsPath(); //NOF.System.getLibsPath();
  178.             for (var i=0; i<libPathsArray.length; i++) {
  179.                 resourceName = libPathsArray[i] + "\\"+resource;
  180.                 var file = null;
  181.                 
  182.                 try{
  183.                     //try to locate the resource in the requested locale
  184.                     file = this.getResourceFile( resourceName +  "_" + locale.toString() );
  185.                     
  186.                     if (file == null)
  187.                         file = this.getResourceFile( resourceName +  "_" + locale.getLanguage() );
  188.                     
  189.                     //try to locate the resource in the default locale
  190.                     if ( file == null && !usingDefault ){
  191.                         file = this.getResourceFile( resourceName +  "_" + defaultLocale.toString() );
  192.                         
  193.                         if (file == null)
  194.                             file = this.getResourceFile( resourceName +  "_" + defaultLocale.getLanguage() );
  195.                     }
  196.                     
  197.                     //finaly try to locate the resource as is
  198.                     if (file == null)   
  199.                         file = this.getResourceFile( resourceName );
  200.                     
  201.                     if (file != null) // a match was found
  202.                         return this.createResource( file, locale );
  203.                     
  204.                 }catch(e){}
  205.             }
  206.             
  207.             return null; //no match found at all. give up
  208.         }
  209.         
  210.         // private methods
  211.         /**
  212.           *  Create PropertyResourceBundle from file name
  213.           **/
  214.         method.createResource = function ( /*NOF.IO.File*/ file, /*NOF.UTIL.Locale*/ locale) {
  215.             var resObjName = file.pathName;
  216.             var content    = file.readContent();
  217.             re = /\.js$/;
  218.             if( re.test( resObjName) ){
  219.                 try{
  220.                     eval(content);
  221.                     var z; 
  222.                     eval("z = new "+resObjName+"()");   
  223.                     return z;
  224.                 }catch( notPropertyResourceBundle ){
  225.                     return null;
  226.                 }
  227.             }else{
  228.                 if( content != null ){
  229.                     //it contains property.key and property.value under properties node   
  230.                     //alert("resObjName = " + resObjName);
  231.                     var toRetObj = new NOF.UTIL.PropertyResourceBundle( resObjName );
  232.                     this.getProperties(content, toRetObj, locale);
  233.                     return toRetObj;
  234.                 }
  235.             }
  236.             return null;
  237.         }
  238.         
  239.         /**
  240.           * Create and return NOF.IO.File object based on file name.
  241.           **/
  242.         method.getResourceFile = function ( /*string*/ root ){
  243.             var file = new NOF.IO.File(  root + ".js" );
  244.             if ( file.exists() ) 
  245.                 return file;
  246.             
  247.             file = new NOF.IO.File(  root + ".properties" );
  248.             if ( file.exists() ) 
  249.                 return file;
  250.             
  251.             return null;
  252.         }
  253.     }
  254.     
  255.     UTIL.__proto__.ResourceBundle = new UTIL_ResourceBundle();    
  256. }
  257.